var config=require('config');
cc.Class({
    extends: cc.Component,

    properties: {
        itemdata:null,
        item1:{
            default:null,
            type:cc.Node
        },
        item2:{
            default:null,
            type:cc.Node
        },
        item3:{
            default:null,
            type:cc.Node
        },
        item4:{
            default:null,
            type:cc.Node
        },
        item5:{
            default:null,
            type:cc.Node
        },
        item6:{
            default:null,
            type:cc.Node
        },
        item7:{
            default:null,
            type:cc.Node
        },
        nameLabel:{
            default: null,
            type: cc.Label
        },
        scoreLabel: {
            default: null,
            type: cc.Label
        },
        timeLabel1: {
            default: null,
            type: cc.Label
        },
        timeLabel2: {
            default: null,
            type: cc.Label
        },
        timeLabel3: {
            default: null,
            type: cc.Label
        },
        timeLabel4: {
            default: null,
            type: cc.Label
        },
        timeLabel5: {
            default: null,
            type: cc.Label
        },
        timeLabel6: {
            default: null,
            type: cc.Label
        },
        timeLabel7: {
            default: null,
            type: cc.Label
        },
        detailLabel1: {
            default: null,
            type: cc.Node
        },
        detailLabel2: {
            default: null,
            type: cc.Node
        },
        detailLabel3: {
            default: null,
            type: cc.Node
        },
        detailLabel4: {
            default: null,
            type: cc.Node
        },
        detailLabel5: {
            default: null,
            type: cc.Node
        },
        detailLabel6: {
            default: null,
            type: cc.Node
        },
        detailLabel7: {
            default: null,
            type: cc.Node
        }
    },

    // use this for initialization
    onLoad: function () {

    },
    
    getDataFromServer: function(){
        var self = this;
        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
                var response = xhr.responseText;
                console.log("draw : " +response);
                var res = JSON.parse(response );  
                self.showDataStyle(res);
            }
        };
        // http://mysite/drawserver/gethistory.php
        // url....com/draw/server/gethistory.php
        var userid = config.userid;
        xhr.open("GET", "url....com/draw/server/gethistory.php?userid="+userid, true);
        xhr.send();
        
    },
    
    showDataStyle: function(data){
        this.nameLabel.string = data['name'];
        this.scoreLabel.string = data['score'];
        this.itemdata = data['data'];
        var count = data['data'].length;
        if(count ==1){
         this.item1.opacity = 255;
         this.timeLabel1.string = data['data'][0]['update_time'];
        }
        if(count ==2){
         this.item1.opacity = 255;
         this.timeLabel1.string = data['data'][0]['update_time'];
         this.item2.opacity = 255;
         this.timeLabel2.string = data['data'][1]['update_time'];
        }
        if(count ==3){
         this.item1.opacity = 255;
         this.timeLabel1.string = data['data'][0]['update_time'];
         this.item2.opacity = 255;
         this.timeLabel2.string = data['data'][1]['update_time'];
         this.item3.opacity = 255;
         this.timeLabel3.string = data['data'][2]['update_time'];
        }
        if(count ==4){
         this.item1.opacity = 255;
         this.timeLabel1.string = data['data'][0]['update_time'];
         this.item2.opacity = 255;
         this.timeLabel2.string = data['data'][1]['update_time'];
         this.item3.opacity = 255;
         this.timeLabel3.string = data['data'][2]['update_time'];
         this.item4.opacity = 255;
         this.timeLabel4.string = data['data'][3]['update_time'];
        }
        if(count ==5){
         this.item1.opacity = 255;
         this.timeLabel1.string = data['data'][0]['update_time'];
         this.item2.opacity = 255;
         this.timeLabel2.string = data['data'][1]['update_time'];
         this.item3.opacity = 255;
         this.timeLabel3.string = data['data'][2]['update_time'];
         this.item4.opacity = 255;
         this.timeLabel4.string = data['data'][3]['update_time'];
         this.item5.opacity = 255;
         this.timeLabel5.string = data['data'][4]['update_time'];
        }
        if(count ==6){
         this.item1.opacity = 255;
         this.timeLabel1.string = data['data'][0]['update_time'];
         this.item2.opacity = 255;
         this.timeLabel2.string = data['data'][1]['update_time'];
         this.item3.opacity = 255;
         this.timeLabel3.string = data['data'][2]['update_time'];
         this.item4.opacity = 255;
         this.timeLabel4.string = data['data'][3]['update_time'];
         this.item5.opacity = 255;
         this.timeLabel5.string = data['data'][4]['update_time'];
         this.item6.opacity = 255;
         this.timeLabel6.string = data['data'][5]['update_time'];
         
        }
        if(count ==7){
         this.item1.opacity = 255;
         this.timeLabel1.string = data['data'][0]['update_time'];
         this.item2.opacity = 255;
         this.timeLabel2.string = data['data'][1]['update_time'];
         this.item3.opacity = 255;
         this.timeLabel3.string = data['data'][2]['update_time'];
         this.item4.opacity = 255;
         this.timeLabel4.string = data['data'][3]['update_time'];
         this.item5.opacity = 255;
         this.timeLabel5.string = data['data'][4]['update_time'];
         this.item6.opacity = 255;
         this.timeLabel6.string = data['data'][5]['update_time'];
         this.item7.opacity = 255;
         this.timeLabel7.string = data['data'][6]['update_time'];
         
        }
    },
    
    showRecordeList: function() {
        this.getDataFromServer();
        this.node.opacity=255;
        this.node.runAction(cc.show());
    },
    
    closeRecordeList: function () {
        // this.node.runAction(cc.moveTo(0.1, cc.p(1500,0)));
        this.node.runAction(cc.hide());
    },
    checkone: function(){
        var data = this.itemdata;
        cc.log(data[0]['serial']);
        window.open(data[0]['serial']);
    },
    checktwo: function(){
        var data = this.itemdata;
        cc.log(data[1]['serial']);
        window.open(data[1]['serial']);
        
    },
    checkthree: function(){
        var data = this.itemdata;
        cc.log(data[2]['serial']);
        window.open(data[2]['serial']);
    },
    checkfour: function(){
        var data = this.itemdata;
        cc.log(data[3]['serial']);
        window.open(data[3]['serial']);
    },
    checkfive: function(){
        var data = this.itemdata;
        cc.log(data[4]['serial']);
        window.open(data[4]['serial']);
    },
    checksix: function(){
        var data = this.itemdata;
        cc.log(data[5]['serial']);
        window.open(data[5]['serial']);
    },
    checkfseven: function(){
        var data = this.itemdata;
        cc.log(data[6]['serial']);
        window.open(data[6]['serial']);
    }
    
    

    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {

    // },
});
